package com.sky.controller;

import com.sky.common.baidu.BaiduService;
//import com.sky.common.oss.OssService;
import com.sky.common.upload.UploadFileService;
import com.sky.config.CommonConfig;
import com.sky.service.impl.RecordUploadService;
import com.sky.util.ChmodUtil;
import com.sky.util.PathUtil;
import com.sky.utils.FileUtils;
import com.sky.vo.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Map;
import java.util.UUID;

/**
 * 百度的api控制层
 * @author majinan
 */
@Log4j2
@RestController
@RequestMapping("/baidu")
@Api(tags = {"百度api控制层"})
public class BaiduController {

    @Autowired
    private UploadFileService uploadFileService;

    @Autowired
    private BaiduService baiduService;

//    @Autowired
//    private OssService ossService;

    @Autowired
    private RecordUploadService uploadService;
    @Autowired
    private CommonConfig properties;

    @PostMapping("/image")
    @ApiOperation(value = "图像识别")
    public AjaxResult uploadImage(@RequestParam("file") MultipartFile file,@RequestParam("userName") String userName) {
        if (file.isEmpty()) {
            return AjaxResult.fail("文件为空");
        }
        // 上传文件
        String fileName = file.getOriginalFilename();//原文件名称
        String uniqueName = UUID.randomUUID() + fileName;


        // 设置保存路径，这里假设保存在项目根目录下的uploads文件夹
        File destFile = new File(Paths.get(properties.getUploadDir(), uniqueName).toAbsolutePath().normalize().toString());
        // 确保目录路径存在
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        // 保存文件
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.fail("文件保存异常");
        }
        ChmodUtil.chmodPath(properties.getUploadDir());
        String path = PathUtil.getPath(properties.getUploadDir(), uniqueName);
        // 对接百度api接口 - 实现图像识别
        Map<String, Object> map;
        try {
            map = baiduService.imageClassify(path,userName);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.fail("文件有问题");
        }
        return AjaxResult.success(map);
    }

//    @PostMapping("/record")
//    @ApiOperation(value = " 通过上传音频进行语音识别其垃圾分类")
//    public AjaxResult uploadRecord(@RequestParam("file") MultipartFile file) {
//        if (file.isEmpty()) {
//            return AjaxResult.fail("文件为空");
//        }
//        byte[] pcmBytes = null;
//        try {
//            pcmBytes = FileUtils.getInputStreamContent(file.getInputStream());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        // 上传文件到oss
//        String path = ossService.fileUpload(file);
//        // 上传到本地
//        String s = uploadService.handleUploadFile(file);
//
//        // 对接百度api接口 - 实现图像识别
//        Map<String, Object> map;
//        try {
//            map = baiduService.speech(path, pcmBytes);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return AjaxResult.fail("文件有问题");
//        }
//        return AjaxResult.success(map);
//    }
}
